//
// Copyright (C) 2006 Pilitsyn Vasiliy <nexus@ablogic.ru>


#include <qapplication.h>
#include <qsessionmanager.h>
#include <qmessagebox.h>
#include <qpixmap.h>
#include <qpopupmenu.h>
#include <qtextcodec.h>




#include "mainwindow.h"
#include "trayicon.h"
#include "msgwin.h"
#include <qstring.h>

class  MyApplication : public QApplication {

   public:

      bool RestartAnyway;
      
      MyApplication(int argc, char** argv)  : QApplication(argc,argv)
      {
     
         QTextCodec *codec=QTextCodec::codecForLocale();
         QTextCodec::setCodecForLocale(codec);
         QTextCodec::setCodecForCStrings(codec);
         QTextCodec::setCodecForTr(codec);

         Pinger scale;
         RestartAnyway = true;
         
         MsgWin hint(0,"", false  );

         setMainWidget(&scale);
    
        
         if (!isSessionRestored())
             scale.show();
         
       
         TrayIcon trayicon;

         QPixmap pix(QPixmap::fromMimeSource("icon.png"));

         QString tip("Network Monitor");

         QAction show_closeMap(0);
         show_closeMap.setText("Open");
         show_closeMap.connect( &show_closeMap, SIGNAL(activated()),&scale,SLOT(hide_show()));


         QAction start_stopMonitoring(0);
         start_stopMonitoring.setText("Ping All");
//       start_stopMonitoring.connect( &start_stopMonitoring, SIGNAL(activated()),&scale,SLOT(pingall()));

         QPopupMenu menu;
         show_closeMap.addTo(&menu);
         start_stopMonitoring.addTo(&menu);


         menu.insertSeparator();
         menu.insertItem("Quit",&scale,SLOT(closeApp()));

    
         trayicon.connect( &trayicon, SIGNAL( doubleClicked( const QPoint& ) ), &scale, SLOT( hide_show() ) );
         scale.connect(&scale, SIGNAL(sendMessadge(const QString &)), &hint, SLOT(setText(const QString &)));
         scale.connect(&scale, SIGNAL(setHideMsgWindow(int &)), &hint, SLOT(setAutoCloseTime(int &)));
        


         trayicon.setToolTip(tip);
         trayicon.setIcon(pix);
         trayicon.setPopup(&menu);
         trayicon.show();
        
          exec();
      }
      
      virtual ~MyApplication()
      
      {

     }

        virtual void commitData(QSessionManager& sm){
           
                   
            if (RestartAnyway == true){
               sm.setRestartHint ( QSessionManager::RestartAnyway );
           }else{
               sm.setRestartHint ( QSessionManager::RestartNever );
           }
 
            
//            scale.closeApp();
           
//             sm.cancel();
          
       }
       


    
};




int main(int argc, char** argv)
{
    MyApplication app(argc,argv);
      
//     return app.exec();
  
}

